package com.twb.system.controller;

import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;
import com.twb.fileCenter.entity.vo.ExportExcelVo;
import com.twb.system.entity.twbEnclosure.TwbEnclosure;
import com.twb.system.entity.twbEnclosure.dto.GetTwbEnclosureByIdDto;
import com.twb.system.entity.twbEnclosure.dto.ListServerFilesDto;
import com.twb.system.entity.twbEnclosure.dto.SearchTwbEnclosureDto;
import com.twb.system.entity.twbEnclosure.vo.SaveTwbEnclosureVo;
import com.twb.system.entity.twbEnclosure.vo.SearchTwbEnclosureVo;
import com.twb.system.entity.twbEnclosure.vo.UpdateTwbEnclosureByIdVo;
import com.twb.system.service.ITwbEnclosureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author DHL
 * @Title TwbEnclosureController
 * @Description 附件信息
 * @date 2020-08-24 10:52:32
 */
@RestController
@RequestMapping("/system/twbEnclosure")
@Api(tags = "附件信息", value = "TwbEnclosureController")
public class TwbEnclosureController extends BaseController {

    @Autowired
    ITwbEnclosureService twbEnclosureService;

    /**
     * @param vo
     * @return
     * @Title search
     * @Description 分页查询
     * @author DHL
     * @date 2020-08-24 10:52:32
     */
    @Auth(verifyToken = true)
    @GetMapping("/search")
    @ApiOperation(value = "分页查询")
    public Result<TwbPageInfo<SearchTwbEnclosureDto>> search(SearchTwbEnclosureVo vo) {
        TwbPageInfo<SearchTwbEnclosureDto> result = twbEnclosureService.searchTwbEnclosure(vo);
        return Results.successWithData(result);
    }

    /**
     * @param id
     * @return
     * @Title getById
     * @Description 根据ID获取详情
     * @author DHL
     * @date 2020-08-24 10:52:32
     */
    @Auth(verifyToken = true)
    @GetMapping("/getById")
    @ApiOperation(value = "根据ID获取详情")
    public Result<GetTwbEnclosureByIdDto> getById(@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String id) {
        TwbEnclosure twbEnclosure = twbEnclosureService.getById(id);
        if (twbEnclosure == null) {
            return Results.failure("数据不存在");
        }
        GetTwbEnclosureByIdDto dto = new GetTwbEnclosureByIdDto();
        BeanUtils.copyNotNullProperties(twbEnclosure, dto);
        return Results.successWithData(dto);
    }

    /**
     * @param vo
     * @return
     * @Title save
     * @Description 保存信息
     * @author DHL
     * @date 2020-08-24 10:52:32
     */
    @Auth(verifyToken = true)
    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public Result<TwbEnclosure> save(@Valid SaveTwbEnclosureVo vo) {
        TwbEnclosure record = new TwbEnclosure();
        record.setName(vo.getName()); // 此处需要修改为需要校验重复的字段
        record.setIsdel(0);
        long count = twbEnclosureService.count(record);
        if (count > 0) {
            return Results.failure("[" + vo.getName() + "]已存在");
        }
        TwbEnclosure saveTwbEnclosure = twbEnclosureService.saveTwbEnclosure(vo);
        if (saveTwbEnclosure != null) {
            return Results.successWithData(saveTwbEnclosure, "保存成功");
        } else {
            return Results.failure("保存失败");
        }
    }

    /**
     * @param vo
     * @return
     * @Title updateById
     * @Description 更新信息
     * @author DHL
     * @date 2020-08-24 10:52:32
     */
    @Auth(verifyToken = true)
    @PutMapping("/updateById")
    @ApiOperation(value = "更新信息")
    public Result<String> updateById(@Valid UpdateTwbEnclosureByIdVo vo) {
        int result = twbEnclosureService.updateTwbEnclosureById(vo);
        if (result > 0) {
            return Results.success("更新成功");
        } else {
            return Results.failure("更新失败");
        }
    }

    /**
     * @param id
     * @return
     * @Title deleteById
     * @Description 删除信息
     * @author DHL
     * @date 2020-08-24 10:52:32
     */
    @Auth(verifyToken = true)
    @PostMapping("/deleteById")
    @ApiOperation(value = "删除信息")
    public Result<String> deleteById(@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String id) {
        int result = twbEnclosureService.deleteTwbEnclosureById(id);
        if (result > 0) {
            return Results.success("删除成功");
        } else {
            return Results.failure("删除失败");
        }
    }

    /**
     * @param ids
     * @return
     * @Title deleteBatchByIds
     * @Description 批量删除信息
     * @author DHL
     * @date 2020-08-24 10:52:32
     */
    @Auth(verifyToken = true, verifyURL = false)
    @PostMapping("/batchDeleteByIds")
    @ApiOperation(value = "批量删除信息")
    public Result<String> deleteBatchByIds(@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String ids) {
        int result = twbEnclosureService.deleteTwbEnclosureByIds(ids);
        if (result > 0) {
            return Results.success("删除成功");
        } else {
            return Results.failure("删除失败");
        }
    }

    /**
     * @param id
     * @return
     * @Title logicDeleteById
     * @Description 逻辑删除信息
     * @author DHL
     * @date 2020-08-24 10:52:32
     */
    @Auth(verifyToken = true)
    @PostMapping("/logicDeleteById")
    @ApiOperation(value = "逻辑删除信息")
    public Result<String> logicDeleteById(@NotBlank @RequestParam String id) {
        TwbEnclosure record = twbEnclosureService.getById(id);
        if (null == record) {
            return Results.failure("信息不存在");
        }
        twbEnclosureService.setUpdateInfo(record);
        record.setIsdel(1);
        boolean result = twbEnclosureService.updateById(record);
        if (result) {
            return Results.success("删除成功");
        } else {
            return Results.failure("删除失败");
        }
    }

    /**
     * @param ids
     * @return
     * @Title logicBatchDeleteByIds
     * @Description 批量逻辑删除
     * @author DHL
     * @date 2020-08-24 10:52:32
     */
    @Auth(verifyToken = true, verifyURL = false)
    @PostMapping("/logicBatchDeleteByIds")
    @ApiOperation(value = "逻辑删除信息")
    public Result<String> logicBatchDeleteByIds(@NotBlank @RequestParam String ids) {
        int result = twbEnclosureService.logicBatchDeleteTwbEnclosureByIds(ids);
        if (result > 0) {
            return Results.success("删除成功");
        } else {
            return Results.failure("删除失败");
        }
    }

    /**
     * @param exportExcelVo
     * @param searchVo
     * @param request
     * @return
     * @Title exportExcel
     * @Description 导出excel
     * @author DHL
     * @date 2020-08-24 10:52:32
     */
    @Auth(verifyURL = false)
    @PostMapping("/exportExcel")
    @ApiOperation(value = "导出excel")
    public Result<String> exportExcel(ExportExcelVo exportExcelVo, SearchTwbEnclosureVo searchVo, HttpServletRequest request) {

        return Results.success();
    }

    /**
     * @param ids
     * @return
     * @Title listServerFiles
     * @Description 批量查询
     * @author DSY
     * @date 2020年1月12日 下午8:11:51
     */
    @Auth(verifyURL = false)
    @GetMapping("/serverFiles")
    public Result<List<ListServerFilesDto>> listServerFiles(
        @RequestParam(required = false) String fileIds,
        @RequestParam(required = false) String bizId,
        @RequestParam(required = false) String bizType) {

        if (StringUtils.isBlank(fileIds) && StringUtils.isBlank(bizId)) {
            return Results.failure("参数为空!");
        }

        SearchTwbEnclosureVo searchTwbEnclosureVo = new SearchTwbEnclosureVo();
        searchTwbEnclosureVo.setBizId(bizId);
        searchTwbEnclosureVo.setBizType(bizType);
        searchTwbEnclosureVo.setFileIds(fileIds);

        List<ListServerFilesDto> list = twbEnclosureService.listServerFiles(searchTwbEnclosureVo);
        return Results.successWithData(list);
    }

    /**
     * @param vo
     * @return
     * @Title searchList
     * @Description 根据BizId查询集合
     * @author DHL
     * @date 2020年11月11日 下午3:33:30
     */
    @Auth(verifyURL = false)
    @GetMapping("/searchListByBizId")
    @ApiOperation(value = "分页查询")
    public Result<List<SearchTwbEnclosureDto>> searchListByBizId(@NotBlank @RequestParam String bizId, @RequestParam String bizType) {

        SearchTwbEnclosureVo vo = new SearchTwbEnclosureVo();
        vo.setBizId(bizId);

        if (StringUtils.isNotBlank(bizType)) {
            vo.setBizType(bizType);
        }

        List<SearchTwbEnclosureDto> list = twbEnclosureService.searchTwbEnclosureList(vo);
        return Results.successWithData(list);
    }
}
